import { comment } from 'postcss'
import request from '../../utils/request.js'


const state = {
	headerNav:[],
	swiperList:[],
	navList:[],
	bannerListData:{},
	advListData:{},
	addressList:[],
	showItemList:[]
}
const mutations = {
	SET_HEADERNAV(state,headerNav){
		state.headerNav = headerNav
	},
	SET_SWIPERLIST(state,swiperList){
		state.swiperList = swiperList
	},
	SET_NAVLIST(state,navList){
		state.navList = navList
	},
	SET_BANNERLIST(state,bannerListData){
		state.bannerListData = bannerListData
	},
	SET_ADVLISTDATA(state,advListData){
		state.advListData = advListData
	},
	SET_ADDRESSLIST(state,addressList){
		state.addressList = addressList
	},
	SET_SHOWITEMLIST(state,showItemList){
		state.showItemList = showItemList
	}
}
const actions = {
	async getHeaderNav({commit}){
			try{
				const result = await request('/getHeaderNavList')
				// console.log(result)
				commit('SET_HEADERNAV',result)
			}catch(e){
				return Promise.reject(e)
			}
	},
	async getSwiperList({commit}){
			try{
				const result = await request('/getIndexSwiperList')
				// console.log(result)
				commit('SET_SWIPERLIST',result)
			}catch(e){
				return Promise.reject(e)
			}
	},
	async getNavList({commit}){
		try{
			const result = await request('/getIndexNavList')
			// console.log(result)
			commit('SET_NAVLIST',result)
		}catch(e){
			return Promise.reject(e)
		}
	},
	async getBannerList({commit}){
		try{
			const result = await request('/getIndexBannerList')
			// console.log(result)
			commit('SET_BANNERLIST',result)
		}catch(e){
			return Promise.reject(e)
		}
	},
	async getAdvListData({commit}){
		try{
			const result = await request('/getIndexAdv')
			// console.log(result)
			commit('SET_ADVLISTDATA',result)
		}catch(e){
			return Promise.reject(e)
		}
	},
	async getAddressList({commit}){
		try{
			const result = await request('/getIndexAddressList')
			// console.log(result)
			commit('SET_ADDRESSLIST',result)
			return result
		}catch(e){
			return Promise.reject(e)
		}
	},
	async getShowItemList({commit}){
		try{
			const result = await request('/getIndexShowItem')
			// console.log(result)
			commit('SET_SHOWITEMLIST',result)
			return 'ok'
		}catch(e){
			return Promise.reject(e)
		}	
	}
}
const getters = {
	bannerGoods(state){
		return state.bannerListData.goods || {}
	},
	goodsDetail(state){
		return state.bannerListData.details || []
	},
	advGoods(state){
		return state.advListData.goods
	},
	config(state){
		return state.advListData.config || {}
	},
	showItemLeft(state){
		return state.showItemList.filter((item,index) => {
			return index % 2 === 0 
		})
	},
	showItemRight(state){
		return state.showItemList.filter((item,index) => {
			return index % 2 !== 0
		})
	}
}

export default {
	namespaced:true,
	state,
	mutations,
	actions,
	getters
}